package com.nbsaas.boot.monitor;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;

@Configuration
public class DisruptorInitializer {

    private static final int RING_BUFFER_SIZE = 16384;

    @Bean
    public Disruptor<ApiLogEvent> apiLogDisruptor() {
        Disruptor<ApiLogEvent> disruptor = new Disruptor<>(
                new ApiLogEventFactory(),
                RING_BUFFER_SIZE,
                Executors.defaultThreadFactory(),
                ProducerType.MULTI,
                new BlockingWaitStrategy()
        );
        disruptor.handleEventsWith(new ApiLogEventHandler());
        disruptor.start();
        return disruptor;
    }

    @Bean
    public ApiMonitorDisruptor apiMonitorDisruptor(Disruptor<ApiLogEvent> disruptor) {
        return new ApiMonitorDisruptor(disruptor);
    }
}